#ifndef SINGLETON_H
#define SINGLETON_H

#include "Config/Export.h"
#include <cstdlib>

namespace blue {

template <class T>
class SHARED Singleton
{
    public:

        static T* instance()
        {
            if(!inst)
                inst = new T;
            return inst;
        }

        static void destroy()
        {
            if(inst)
                delete inst;
            inst = NULL;
        }

    protected:

        Singleton() {}
        ~Singleton() {}

    private:

        static T* inst;
        Singleton(const Singleton&);
        Singleton& operator =(const Singleton&);
};

#define MAKE_SINGLETON(Class) \
        public : \
            friend class Singleton<Class>; \
            static Class* instance(); \
            static void destroy(); \
        private : \
            Class(); \
            ~Class();

#define SINGLETON_IMPL(Class) \
        template <> Class* Singleton<Class>::inst = NULL; \
        Class* Class::instance() \
        { \
            return Singleton<Class>::instance(); \
        } \
        void Class::destroy() \
        { \
            Singleton<Class>::destroy(); \
        }
}

#endif
